function uuid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
    }
    return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}

function HexToBase64(hex) {
    var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64 = "";
    var group;
    for (var i = 0; i < 30; i += 6) {
        group = parseInt(hex.substr(i, 6), 16);
        base64 += base64Digits[(group >> 18) & 0x3f];
        base64 += base64Digits[(group >> 12) & 0x3f];
        base64 += base64Digits[(group >> 6) & 0x3f];
        base64 += base64Digits[group & 0x3f];
    }
    group = parseInt(hex.substr(30, 2), 16);
    base64 += base64Digits[(group >> 2) & 0x3f];
    base64 += base64Digits[(group << 4) & 0x3f];
    base64 += "==";
    return base64;
}

function NUUID(uuid) {
    var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
    var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
    var b = hex.substr(10, 2) + hex.substr(8, 2);
    var c = hex.substr(14, 2) + hex.substr(12, 2);
    var d = hex.substr(16, 16);
    hex = a + b + c + d;
    var base64 = HexToBase64(hex);
    return new BinData(3, base64);
}

function addCriteria(){
    var database = db.getSiblingDB("ClientPlatform");
    var criteriaLevel2Id = database.CriteriaLevel2.findOne({ Name: "Business overview"})._id;
    var criteriaLevel3 = database.CriteriaLevel3.findOne({CriteriaLevel2Id: criteriaLevel2Id, Name: "Bloomberg Ticker Exchange"});
    
    if (criteriaLevel3 == undefined){
        criteriaLevel3 = {
            "_id" : NUUID(uuid()),
            "CriteriaLevel2Id" : criteriaLevel2Id,
            "Name" : "Bloomberg Ticker Exchange",
            "ShortName" : "",
            "Order" : 771,
            "Category" : "None"
        }
        
        database.CriteriaLevel3.update({Name: "Bloomberg Ticker"}, { $set: { Order: 770 }})
        database.CriteriaLevel3.insert(criteriaLevel3);
        
        print("Added criteria level 3 for Bloomberg ticker and exchange.")
        
        database.TemplateCriteria.insert({
            _id: NUUID(uuid()),
            Name: "Bloomberg Ticker Exchange",
            CriteriaEntityField: "Bloomberg Ticker Exchange",
            CriteriaType: NumberInt(3),
            CriteriaLevel2Id: criteriaLevel2Id,
            CriteriaLevel3Id: criteriaLevel3._id,
            IsScreenable: false,
            ResearchProducts: null,
            HasFullRangeChart: false,
            HasOwnValue : true
        });
        
        print("Added template criteria for Bloomberg ticker and exchange.")
    }
    else {
        print("Criteria already exists")
    }
}

addCriteria();